package com.kly.sc.api.application.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.kly.sc.api.application.controller"))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(setHeaderToken());

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("action-swagger").description("meboo api").termsOfServiceUrl("")
                .version("3.2").build();
    }

    /**
     * 设置swagger文档中全局参数
     */
    private List<Parameter> setHeaderToken() {
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        parameterBuilder.name("basicParams").description("basicParams").defaultValue("{\"timestamp\":1698131218597,\"channel\":\"l1\",\"appsFlyerUID\":\"\",\"appCode\":\"sparksy\",\"applicationID\":\"com.kly.meboo\",\"appVersion\":37,\"token\":\"eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJzdWIiOiIxNzE2NzEyMjIwODkxNjQzOTA0IiwianRpIjoiMTcxNjcxMjIyMDg5MTY0MzkwNCIsImlzcyI6InNlY3VyaXR5IiwiaWF0IjoxNjk4MTMxMDYzLCJhdWQiOiJzZWN1cml0eS1hbGwiLCJleHAiOjE2OTg3MzU4NjN9.FQZYY4zsxfOkfVAHwB7PXHTaoP-FybQijG1eQJI9GVM\",\"appVersionName\":\"2.5.3\",\"appSource\":\"1\"}").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
        pars.add(parameterBuilder.build());
        return pars;
    }
}
